# -*- coding: utf-8 -*-
######################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
# Copyright (C) 2013 INIT Tech Co., Ltd (http://init.vn).
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
######################################################################

from osv import fields,osv
import pooler
from tools.translate import _

class init_SmtpClientEmailTemplate(osv.osv):
    _inherit = 'email.smtpclient.email.template'
    _description = 'Email Template'
    _columns = {
        'file_name':fields.char(
                'Report Filename',
                size=200,
                help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
                translate=True),
        'report_template':fields.many2one(
                  'ir.actions.report.xml',
                  'Report to send'),
        'model_id':fields.many2one(
                                  'ir.model',
                                  'Model'),
        'line_ids': fields.one2many('init.report.condition', 'template_id', 'Lines'),
    }
    
    def onchange_report_template(self, cr, uid, ids, report_template):
        if not report_template:
            return {'value': {}}
        model_name = self.pool.get('ir.actions.report.xml').browse(cr, uid, report_template).model
        model = self.pool.get('ir.model')
        model_ids = model.search(cr, uid, [('model', '=', model_name)])
        if model_ids:
            return {'value': {'model_id': model_ids[0]}}
        return {'value': {}}


init_SmtpClientEmailTemplate()

class init_report_condition(osv.osv):
    _name = 'init.report.condition'

    _columns = {
        'value':fields.char('Value', size=200),
        'daily':fields.boolean('Now'),
        'field_id':fields.many2one('ir.model.fields', 'Condition', required=True, domain="[('model_id','=',parent.model_id)]"),
        'template_id':fields.many2one('email.smtpclient.email.template', 'Template', required=True),        
    }


init_report_condition()



